﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bundled.Entity;
using System.Data;
using System.Web.UI.HtmlControls;

namespace Bundled.Pages.Cart
{
    public partial class Default : System.Web.UI.Page
    {
        ShoppingCart sc;
        const decimal SALES_TAX = .06m;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                sc = ShoppingCart.GetCart();
                
                gvCart.DataSource = sc.ItemList;
                gvCart.DataBind();

                decimal subTotal = sc.Total;
                if (subTotal == 0m)
                    divTotals.Visible = false;
                else
                    divEmpty.Visible = false;

                lblSubtotal.Text = subTotal.ToString("c2");
                decimal tax = subTotal * SALES_TAX;
                lblTax.Text = tax.ToString("c2");
                lblTotal.Text = (subTotal + tax).ToString("c2");

                int i = 1;
                foreach (var item in sc.ItemList)
                {
                    Literal litName = new Literal();
                    litName.Text = @"<input type=""hidden"" name=""item_name_" + i + '"' + @" value=""" + item.PartName + @""" />";
                    divPayPal.Controls.Add(litName);

                    Literal litPrice = new Literal();
                    litPrice.Text = @"<input type=""hidden"" name=""amount_" + i + '"' + @" value=""" + item.Price + @""" />";
                    divPayPal.Controls.Add(litPrice);

                    Literal litQty = new Literal();
                    litQty.Text = @"<input type=""hidden"" name=""quantity_" + i + '"' + @" value=""" + item.Quantity + @""" />";
                    divPayPal.Controls.Add(litQty);

                    i++;
                }

                Literal litTaxName = new Literal();
                litTaxName.Text = @"<input type=""hidden"" name=""item_name_" + i + '"' + @" value=""Tax"" />";
                divPayPal.Controls.Add(litTaxName);

                Literal litTaxAmount = new Literal();
                litTaxAmount.Text = @"<input type=""hidden"" name=""amount_" + i + '"' + @" value=""" + tax + @""" />";
                divPayPal.Controls.Add(litTaxAmount);
            }
        }

        protected void btnRemove_Command(object sender, CommandEventArgs e)
        {
            int id = Convert.ToInt32(e.CommandArgument);

            ShoppingCart.RemoveFromCart(id);
            Response.Redirect("~/Pages/Cart/");
        }

        protected void btnUpdate_Command(object sender, CommandEventArgs e)
        {
            string[] arg = new string[2];
            arg = e.CommandArgument.ToString().Split(',');
            int id = Convert.ToInt32(arg[0]);
            int index = Convert.ToInt32(arg[1]);
            GridViewRow row = gvCart.Rows[index];
            int qty = Convert.ToInt32(((TextBox)(row.Cells[1].Controls[1])).Text);

            ShoppingCart.UpdateCart(id, qty);
            Response.Redirect("~/Pages/Cart/");
        }
    }
}